﻿#if L2S
using System.Data.Linq;
#endif

#if EGOAL
using DataContext = EgoalTech.MTrade.Query.ObjectDataContext;
#endif

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace EgoalTech.MTrade.Query
{
    interface IDataSourceProvider
    {
        IQueryable GetSource(string symbolName);
    }

    class LinqSourceProvider : IDataSourceProvider
    {
        private object dataContext;
        private Type dataContextType;

        public LinqSourceProvider(object dataContext)
        {
            if (dataContext == null)
                throw Error.ArgumentNull("dataContext");

            this.dataContext = dataContext;
            this.dataContextType = dataContext.GetType();
        }

        public IQueryable GetSource(string symbolName)
        {
            IQueryable dataSource = null;
            var p = dataContextType.GetProperty(symbolName);//TODO:判断返回的类型是否为 IQueryable
            if (p != null)
            {
                dataSource = (IQueryable)p.GetValue(dataContext, null);
            }

            var f = dataContextType.GetField(symbolName);//TODO:判断返回的类型是否为 IQueryable
            if(f != null)
            {
                dataSource = (IQueryable) f.GetValue(dataContext);
            }

            //TODO:dataSource 为空抛异常

            return dataSource;
        }

    }
}
